﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Text;
using System.Data;
using System.Web.Configuration;

namespace ReconUI
{
    public partial class InvoiceCreditNote : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (((UserVO)Session["CurrentUserVO"]) == null)
            {
                Response.Redirect("ReconSignIn.aspx");
            }
        }
        protected void btnInvoiceCancel_Click(object sender, EventArgs e)
        {
            ResetFieldsInvoice();
        }
        protected void btnInvoice_Click(object sender, EventArgs e)
        {
            DBFactory db = new DBFactory();
            int is_Partial = 0;

            if (chkIsPartialInvoice.Checked)
                is_Partial = 1;

            int retval = db.AddInvoice(Convert.ToString(txtRemitterId.Text.Trim()), Convert.ToString(txtICustomerId.Text.Trim()), Convert.ToString(txtInvoiceNo.Text.Trim()), ConvertDate(txtInvoiceDate.Value),
                    Convert.ToString(txtCurrency.Text.Trim()),
                    Convert.ToString(txtIAmount.Text.Trim()), Convert.ToString(txtIRefNoChqNo.Text.Trim()), Convert.ToInt32(txtServiceTax.Text.Trim()),
                     Convert.ToInt32(txtVAT.Text.Trim()), Convert.ToInt32(txtCess.Text.Trim()), Convert.ToInt32(txtSalesTax.Text.Trim()),
                     Convert.ToInt32(txtTax1.Text.Trim()), Convert.ToInt32(txtTax2.Text.Trim()), Convert.ToInt32(txtTax3.Text.Trim()),
                     Convert.ToInt32(txtDiscount.Text.Trim()), "N", is_Partial);

            if (retval != 0)
            {
                //ClientScript.RegisterClientScriptBlock(this.GetType(), "SuccessMessage", "<script>alert('Statement added successfully!')</script>");
                lblMessage.Text = "Invoice added successfully!";
                btnClickPopup.Show();
                ResetFieldsInvoice();
            }
            else
            {
                //ClientScript.RegisterClientScriptBlock(this.GetType(), "ErrorMessage", "<script>alert('Error adding Statement!')</script>");
                lblMessage.Text = "Error adding Invoice!";
                btnClickPopup.Show();
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            DBFactory db = new DBFactory();
            int is_Partial = 0;

            if (chkIsPartial.Checked)
                is_Partial = 1;

            int retval = db.AddStatement(Convert.ToString(txtCustomerId.Text.Trim()), Convert.ToString(txtBankId.Text.Trim()), ConvertDate(txtStatementDt.Value),
                    ConvertDate(txtPostingDt.Value), ConvertDate(txtValueDt.Value), Convert.ToString(txtTransactipnType.Text.Trim()),
                    Convert.ToString(txtNaration1.Text.Trim()), Convert.ToString(txtNaration2.Text.Trim()), Convert.ToString(txtNaration3.Text.Trim()),
                    Convert.ToString(txtNaration4.Text.Trim()), Convert.ToString(txtAmount.Text.Trim()), Convert.ToString(txtRefNoChqNo.Text.Trim()),
                    Convert.ToString(txtReconciledAmount.Text.Trim()), Convert.ToString(txtStatementStatus.Text.Trim()), is_Partial);

                if (retval != 0)
                {
                    //ClientScript.RegisterClientScriptBlock(this.GetType(), "SuccessMessage", "<script>alert('Statement added successfully!')</script>");
                    lblMessage.Text = "Statement added successfully!";
                    btnClickPopup.Show();
                    ResetFields();
                }
                else
                {
                    //ClientScript.RegisterClientScriptBlock(this.GetType(), "ErrorMessage", "<script>alert('Error adding Statement!')</script>");
                    lblMessage.Text = "Error adding Statement!";
                    btnClickPopup.Show();
                }
         
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ResetFields();
        }

        public string ConvertDate(string yyyyMMdd)
        {
            string[] str;
            string s = yyyyMMdd;
            str = s.Split('-').ToArray();

            if (str[0].Length == 1)
            { str[0] = "0" + str[0]; }

            if (str[1].Length == 1)
            { str[1] = "0" + str[1]; }

                s = str[2] + "-" + str[1] +  "-" + str[0];
                return s;

        }
    

        private void ResetFields()
        {
            txtCustomerId.Text = "";
            txtBankId.Text = "";
            txtStatementDt.Value = "";
            txtPostingDt.Value = "";
            txtValueDt.Value = "";
            txtTransactipnType.Text = "";
            txtNaration1.Text = "";
            txtNaration2.Text = "";
            txtNaration3.Text = "";
            txtNaration4.Text = "";
            txtAmount.Text = "";
            txtRefNoChqNo.Text = "";
            txtReconciledAmount.Text = "";
            txtStatementStatus.Text = "";
            chkIsPartial.Checked = false;
        }

        private void ResetFieldsInvoice()
        {      
            txtICustomerId.Text = "";
            txtRemitterId.Text = "";
            txtInvoiceDate.Value = "";
            txtInvoiceNo.Text = "";
            txtCurrency.Text = "";
            txtIAmount.Text = "";
            txtIRefNoChqNo.Text = "";
            txtServiceTax.Text = "";
            txtVAT.Text = "";
            txtCess.Text = "";
            txtSalesTax.Text = "";
            txtTax1.Text = "";
            txtTax2.Text = "";
            txtTax3.Text = "";
            txtDiscount.Text = "";
            chkIsPartialInvoice.Checked = false;
        }

        protected void Button1_Click1(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>window.open('SelectDate.aspx?textbox=" + txtStatementDt.ClientID + "','cal','width=250,height=225,left=480,top=280')</script>");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>window.open('SelectDate.aspx?textbox=" + txtPostingDt.ClientID + "','cal','width=250,height=225,left=750,top=310')</script>");
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>window.open('SelectDate.aspx?textbox=" + txtValueDt.ClientID + "','cal','width=250,height=225,left=480,top=310')</script>");
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>window.open('SelectDate.aspx?textbox=" + txtInvoiceDate.ClientID + "','cal','width=250,height=225,left=750,top=310')</script>");
        }

        protected void btnok_Click(object sender, EventArgs e)
        {
            btnClickPopup.Hide();
        }

        protected void rbOption_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rbOption.SelectedValue == "Invoice")
            {
                pnlInvoice.Visible = true;
                pnlInvoice.Enabled = true;
                pnlStatement.Visible = false;
                pnlStatement.Enabled = false;
            }
            else if (rbOption.SelectedValue == "Credit")
            {
                pnlInvoice.Visible = false;
                pnlInvoice.Enabled = false;
                pnlStatement.Visible = true;
                pnlStatement.Enabled = true;
            }
        }
    }
}